// ignore_for_file: file_names
// https://www.cnblogs.com/MaiJiangDou/p/14060017.html

class TimeUtil {
  String getLocalTime(String time, {String format = ''}) {
    DateTime date = DateTime.fromMillisecondsSinceEpoch(int.parse(time));
    //padLeft(int width,String padding)：如果字符串长度小于width，在左边填充padding
    String year = date.year.toString();
    String month = date.month.toString().padLeft(2, '0');
    String day = date.day.toString().padLeft(2, '0');
    String hour = date.hour.toString().padLeft(2, '0');
    String minute = date.minute.toString().padLeft(2, '0');
    String second = date.second.toString().padLeft(2, '0');

    String timestamp = '$year-$month-$day';
    if (format == '') {
      timestamp = '$timestamp $hour:$minute:$second';
    }

    return timestamp;
  }

  int startTime() {
    var date = new DateTime.now();
    // print(now.millisecondsSinceEpoch);//单位毫秒，13位时间戳
    String year = date.year.toString();
    String month = date.month.toString().padLeft(2, '0');
    String day = date.day.toString().padLeft(2, '0');
    var d1 = '$year-$month-$day 00:00:00';
    return DateTime.parse(d1).millisecondsSinceEpoch;
  }

  int endTime(){
    var date = new DateTime.now();
    // print(now.millisecondsSinceEpoch);//单位毫秒，13位时间戳
    String year = date.year.toString();
    String month = date.month.toString().padLeft(2, '0');
    String day = date.day.toString().padLeft(2, '0');
    var d2 = '$year-$month-$day 24:00:00';
    return DateTime.parse(d2).millisecondsSinceEpoch;
  }

  int startWeek(){
     var current = new DateTime.now();
     final week = current.weekday;
     final date = current.add(Duration(days: (0-week)));
    // print(now.millisecondsSinceEpoch);//单位毫秒，13位时间戳
    String year = date.year.toString();
    String month = date.month.toString().padLeft(2, '0');
    String day = date.day.toString().padLeft(2, '0');
    var d2 = '$year-$month-$day 24:00:00';
    return DateTime.parse(d2).millisecondsSinceEpoch;
  }

  int endWeek(){
     var current = new DateTime.now();
     final week = current.weekday;
     final date = current.add(Duration(days: (7-week)));
    // print(now.millisecondsSinceEpoch);//单位毫秒，13位时间戳
    String year = date.year.toString();
    String month = date.month.toString().padLeft(2, '0');
    String day = date.day.toString().padLeft(2, '0');
    var d2 = '$year-$month-$day 11:59:00';
    return DateTime.parse(d2).millisecondsSinceEpoch;
  }

  String getTimeStamp() {
    final temp = DateTime.now().millisecondsSinceEpoch;
    return temp.toString();
  }
}

// main(List<String> args) {
//   var temp = DateTime.parse('2021-10-31').millisecondsSinceEpoch;

//   // print(TimeUtil().getTimeStamp());
//   // print(TimeUtil().getLocalTime('1635229481202063'));
// }
